//
//  CustomTabbarController.m
//  tarbar-选中状态-背景色
//
//  Created by ZMC on 15/12/6.
//  Copyright © 2015年 Zmc. All rights reserved.
//

#import "CustomTabbarController.h"
#import "CustomTabbar.h"
#import "CustomNavController.h"
#import "appStatus.h"
#import "RootViewController.h"
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
#define SHEIGHT [[UIScreen mainScreen] bounds].size.height
#define SWIDTH [[UIScreen mainScreen] bounds].size.width
#define CHEIGHT [[UIScreen mainScreen] bounds].size.height - 64
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface CustomTabbarController ()<UITabBarControllerDelegate,CustomTabbarDelegate>
{
    NSArray*_tabArray;
    CustomTabbar*_costomtab;
}
@end

@implementation CustomTabbarController
@synthesize delegate;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.hidden = YES;
    NSArray*VcArray=@[@"HomeViewController",
                      @"SupplyOfGoodsViewController",
                      @"PurchasingViewController",
                      @"MyViewController"];
    NSArray*VCTitles=@[@"首页",
                       @"货源",
                       @"采购",
                       @"我的"];
    NSArray*image=@[@"首页",
                    @"货源",
                    @"采购",
                    @"我的",];
    
    
    NSArray*selectImage=@[@"首页选中",
                          @"货源选中",
                          @"采购选中",
                          @"我的选中",];
    
    
    CustomTabbar*customtabbar=[[CustomTabbar alloc]initWithTitles:VCTitles images:image selectedImages:selectImage tabBarStyle:CustomTabbarStyleNormal norTextColor:[UIColor grayColor] selectedTextColor:UIColorFromRGB(0x0078c1) bgColor:[UIColor whiteColor] selectedColor:[UIColor cyanColor]];
    customtabbar.layer.masksToBounds = YES;
    customtabbar.layer.cornerRadius = 0;
    customtabbar.layer.borderWidth = 0.6;
    customtabbar.layer.borderColor = [UIColorFromRGB(0xd2d2d2) CGColor];
    customtabbar.tabbarDelegate=self;
    customtabbar.tabBarFont=[UIFont systemFontOfSize:10];
    [self.view addSubview:customtabbar];
    NSMutableArray*tabArray=[NSMutableArray array];
    for (int i=0; i<VcArray.count;i++) {
        Class cls=NSClassFromString(VcArray[i]);
        UIViewController*vc=[[cls alloc]init];
        CustomNavController*nav=[[CustomNavController alloc]initWithRootViewController:vc];
        [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x0078c1)];
        [tabArray addObject:nav];
        _tabArray=tabArray;
    }
    self.viewControllers=tabArray;
    _costomtab=customtabbar;
}

//点击按钮显示
- (void) clickThisBtnAtIndex:(NSInteger)index{
    [self.navigationController setNavigationBarHidden:NO];
    self.selectedIndex=index;
    if (self.selectedIndex == 3) {
        [self.navigationController setNavigationBarHidden:YES];
//        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"0"] || [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] == nil) {
//            if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(switchRootViewController:)]) {
//                [self.delegate switchRootViewController:@"root"];
//            }
//            
//        }
    }
    
}
- (void)showTabbar{
    _costomtab.hidden=NO;
}
- (void)hiddenTabbar{
    _costomtab.hidden=YES;
}
-(void)switchRootViewController:(NSString *)rootController{
//    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(switchTabController:)]) {
        [self.delegate switchRootViewController:rootController];
//    }
}
-(void)switchTabController:(NSString *)controller{
    [self switchRootViewController:controller];
}
@end
